<template>
<div style="width: 100%">
  <a-card class="card" :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
         <a-row :gutter="48">
           <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="学号">
              <a-input placeholder="请输入..." v-model="queryParam.xh"/>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="姓名">
              <a-input placeholder="请输入..." v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
           <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="学生类别">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.xslbm">
                <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item
                label="学生来源"
              >
                <a-select :allowClear="true" v-model="queryParam.xslxm">
                  <a-select-option v-for="i in TYPE0503" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
         <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="学科门类">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.xkml">
                <a-select-option v-for="i in selectXkConfig" :key="i.id" :value="i.xkdm">{{ i.xkmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
           <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="院系">
                  <a-select :allowClear="true" :disabled="selectYxConfig.length === 1" showSearch optionFilterProp="children" v-model="queryParam.yx" @change="handleSelectYx">
                    <a-select-option v-for="i in selectYxConfig" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
                  </a-select>
                </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="专业">
                <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zy">
                  <a-select-option v-for="i in selectZyConfig" :key="i.zyh" :value="i.zyh">{{ i.zyh+'-'+i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :sm="{span:24}" :xl="{span:advanced?24:12}" style="text-align:center">
            <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button type="primary" @click="handleExport">导出</a-button>
            <a-button type="primary" @click="handleBatchDelete">删除</a-button>
              <a-button @click="queryParam = {};$refs.table.refresh()">重置</a-button>

            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
          </a-col>
        </a-row>
      </a-form>
    </div>
    </a-card>
    <a-card class="card" :bordered="false">
    <STable
      v-if="init"
      ref="table" :alert="options.alert" :rowSelection="options.rowSelection" rowKey="id" size="default" :columns="columns" :data="loadData" :scroll="{ x: 1300 }">
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="xbhssyj" slot-scope="text, record, index">{{ record.typs }}同{{ record.fdps }}反{{ record.qqps }}弃</span>
        <template slot="operation" slot-scope="text, record, index">
          <a @click="handleOpenEdit(record)">修改</a>
      </template>
      </STable>
    </a-card>
   <handleEdit ref="handleEdit" title="学部会表决结果修改" :visible="editVisible" @success="handleEditSuccess" @close="handleEditClose"  :data="currentInfo" />
</div>
</template>

<script>
import handleEdit from '../common/handleEdit.vue'
import { STable } from '~'
import { mapState } from 'vuex'

const selectArr = [
 'TYPE0075',//学生类别
  'TYPE0374'//审核结果
 ]


export default {
  components: {
    STable,
    handleEdit
  },
  data () {
    return {
      editVisible:false,
      advanced: true,
      // 条件配置
      selectXkConfig:[],//学科
      selectYxConfig: [], //院系
      selectZyConfig: [], //专业
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '院系',
          dataIndex: 'ssyxmc'
        },
        {
          title: '专业',
          dataIndex: 'sszymc'
        },
        {
          title: '学科门类',
          dataIndex: 'xkmc'
        },
        {
          title: '学号',
          dataIndex: 'xh'
        },
        {
          title: '答辩人',
          dataIndex: 'xm'
        },
        {
          title: '学生类别',
          dataIndex: 'xslbmc'
        },
        {
          title: '导师',
          dataIndex: 'dsxm'
        },
        {
          title: '学部会审核意见',
          dataIndex: 'shjgmc'
          //scopedSlots: { customRender: 'xbhssyj' }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          fixed:'right',
          width:100
        }
      ],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectedRowKeys: [],
      selectedRows: [],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.$nextTick(() => {
          this.selectedRowKeys = []
          this.$refs.table.clearSelected()
        })
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.degree.auditDivisionQueryList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      //当前项
      currentInfo:{},
      init: false
    }
  },
  computed: {
    ...mapState({
      TYPE0503: state => state.app['TYPE0503'],
      TYPE0075: state => state.app['TYPE0075'] // 学位类别
    })
  },
  created () {
    this.initSelectMenu()
    this.$store.dispatch('app/setDictionary', 'TYPE0503')
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$api.base.subjectAll({xklb: '01'})
      ]
      Promise.all(allRequest).then(res => {
        this.selectYxConfig = res[0].data;
        this.selectXkConfig = res[1].data;
        if (res[0].data.length === 1) {
          this.$set(this.queryParam, 'yx', res[0].data[0].dwh)
          this.handleSelectYx(res[0].data[0].dwh)
        }
        this.init = true
      })
    },
    //表格选择
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    //选择院系
    handleSelectYx(code) {
      this.selectZyConfig = [];
      this.$set(this.queryParam, 'zy', '')
      this.$api.base.majorAll({ lsdwh: code }).then(res => {
        this.selectZyConfig = res.data;
      })
    },
    //修改
    handleEditSuccess(vals){
      let params = Object.assign({},this.currentInfo,vals)
      this.$api.degree.auditDivisionSet(params).then(res=>{
          this.$message.success('修改成功');
          this.$refs.table.refresh(false);
          this.editVisible = false
      })
    },
    //打开修改
    handleOpenEdit(record){
      this.currentInfo = record;
      this.editVisible = true;
    },
    // 关闭添加弹层
    handleEditClose() {
      this.editVisible = false
    },
    //批量删除
    handleBatchDelete(){
      this.$confirm({
        title: '删除提示',
        content: '确认删除选中信息吗?',
        onOk:()=>{
          return new Promise((resolve, reject) => {
            if (!this.selectedRowKeys.length){
              resolve();
              return;
            };
            //缺少接口
            this.$api.degree.auditDivisionDel(this.selectedRowKeys.join(",")).then(res=>{
              this.$message.success('删除成功');
              this.$refs.table.refresh(false);
              resolve();
            })
          }).catch(() => console.log('Oops errors!'));
        },
        onCancel() {},
      });
    },
    // 导出
    handleExport () {
      this.$export('/degree/xwsh/xbhExp', this.queryParam)
    }
  }
}
</script>

<style lang="less" scoped>
.card{
  margin-bottom: 20px;
}
</style>
